<template>
    <div class="asideContent">
        <div class="panel">
            <el-card v-if="showAside">
                <el-tooltip :content="username" placement="right" v-if="headPic">
                    <el-avatar :size="40" :src="url + headPic">
                        <span>blue</span>
                    </el-avatar>
                </el-tooltip>
                <div class="login" @click="toLogin" v-else>
                    <span>登录</span>
                </div>
                <el-divider />
                <div class="box">
                    <el-tooltip content="历史会话" placement="right">
                        <div class="item" @mouseenter="hisChat">
                            <el-icon size="25">
                                <CopyDocument />
                            </el-icon>
                        </div>
                        <template #content>
                            <div>历史会话(最多保留10条)</div>
                            <div class="hisChat" v-for="(item, index) in hisChatList" :key="item.id">
                                <p @click="showChat(item.id)">{{ index + 1 }}.{{ item.sessionNm }}</p>
                                <el-icon @click="deleteHisChat(item.id)">
                                    <Close />
                                </el-icon>
                            </div>
                        </template>
                    </el-tooltip>
                    <el-tooltip content="初始会话" placement="right">
                        <div class="item" @click="clearMessage">
                            <el-icon size="25">
                                <SwitchButton />
                            </el-icon>
                        </div>
                    </el-tooltip>
                    <el-tooltip content="退出登录" placement="right" v-if="token">
                        <div class="item" @click="logOut">
                            <el-icon size="25">
                                <Fold />
                            </el-icon>
                        </div>
                    </el-tooltip>
                    <el-tooltip content="返回上级" placement="right">
                        <div class="item" @click="back">
                            <el-icon size="25">
                                <DArrowLeft />
                            </el-icon>
                        </div>
                    </el-tooltip>
                </div>
            </el-card>
        </div>
        <div class="trigger">
            <el-tooltip content="收起" placement="right" v-if="showAside">
                <el-icon size="25" @click="showAside = false">
                    <ArrowLeftBold />
                </el-icon>
            </el-tooltip>
            <el-tooltip content="展开" placement="right" v-else>
                <el-icon size="25" @click="showAside = true">
                    <ArrowRightBold />
                </el-icon>
            </el-tooltip>
        </div>
    </div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { DArrowLeft, CopyDocument, Fold, ArrowLeftBold, ArrowRightBold, Close, SwitchButton } from '@element-plus/icons-vue'
import api from '@/api'
import VueEvent from '@/utils/event.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { isMobile } from '@/utils/busi';
import { useRouter } from 'vue-router'

const router = useRouter()
const showAside = ref(true)
const url = ref(import.meta.env.VITE_BASE_URL + '/system/file/download/')
const props = defineProps({
    headPic: String,
    username: String,
    token: String
})
const toLogin = () => {
    VueEvent.emit("dialogVisible", true)
}
const emit = defineEmits(['sendData']);
const logOut = () => {
    ElMessageBox.confirm(
        '请确认要退出登录?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        window.localStorage.removeItem("access-admin")
        emit('sendData', {})
        ElMessage.success('退出成功!')
    }).catch(() => { })
}
const back = () => {
    router.push("/home")
}
const hisChatList = ref([])
const hisChatFlag = ref(false)
const hisChat = () => {
    if (hisChatFlag.value) {
        return
    }
    getHisChatList()
    hisChatFlag.value = true
}
const getHisChatList = async () => {
    if (props.token) {
        const res = await api.get('/ai/chatHistory/list')
        if (res.data.code === 200) {
            hisChatList.value = res.data.data
        } else {
            ElMessage.error(res.data.msg)
        }
    }
}
const deleteHisChat = (id) => {
    ElMessageBox.confirm(
        '请确认要删除该条记录?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        const res = await api.post('/ai/chatHistory/delete', { id: id })
        if (res.data.code === 200) {
            ElMessage.success(res.data.msg)
            getHisChatList()
        } else {
            ElMessage.error(res.data.msg)
        }
    }).catch(() => { })
}
const showChat = (historyId) => {
    VueEvent.emit("historyId", historyId)
}
const clearMessage = () => {
    VueEvent.emit("clearMessage")
}
onMounted(() => {
    if (isMobile()) {
        showAside.value = false
    }
    VueEvent.on("hisChatList", () => {
        getHisChatList()
        hisChatFlag.value = true
    })
})
</script>
<style scoped lang="less">
.el-card {
    border-radius: 20px;
}

.asideContent {
    display: flex;
    align-items: center;
    justify-content: center;
}

.hisChat {
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 5px;
    line-height: 10px;
}

.panel {
    min-height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;

    .login {
        text-align: center;
        width: 40px;
        height: 40px;
        line-height: 40px;
        border-radius: 10px;
        cursor: pointer;
    }

    .login:hover {
        background-color: #DCDCDC;
    }

    .box {
        width: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        gap: 40px;


        .item {
            width: 40px;
            height: 40px;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }

        .item:hover {
            background-color: #DCDCDC;
        }
    }
}

.trigger {
    min-height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    /* 元素不可见，但仍然占据空间 */
    transition: opacity 0.5s ease;
    /* 可选的过渡效果 */
}

:hover.trigger {
    opacity: 1;
    /* 元素变为完全不透明 */
}
</style>